import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login'
import Main from "@/views/Main";
import User from "@/views/modules/sys/User"
import Role from "@/views/modules/sys/Role"
import Menu from "@/views/modules/sys/Menu"
import Plot from "@/views/modules/trace/Plot"
import Forest from "@/views/modules/trace/Forest"
import Symptom from "@/views/modules/trace/Symptom"
import Plant from "@/views/modules/trace/Plant"
import UserCenter from "@/views/UserCenter"

Vue.use(VueRouter)

const routes = [
  {
    path: '/main',
    name: 'Main',
    component: Main,
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: {
          title: "首页"
        },
        component: Home
      },
      {
        path: '/userCenter',
        name: 'UserCenter',
        meta: {
          title: "用户中心"
        },
        component: UserCenter
      },
      {
        path: '/sys/users',
        name: 'User',
        meta: {
          title: "首页"
        },
        component: User
      },
      {
        path: '/sys/roles',
        name: 'Home',
        meta: {
          title: "首页"
        },
        component: Role
      },
      {
        path: '/sys/menus',
        name: 'Home',
        meta: {
          title: "首页"
        },
        component: Menu
      },
      {
        path: '/trace/plot',
        name: 'Plot',
        meta: {
          title: "地块管理"
        },
        component: Plot
      },
      {
        path: '/trace/forest',
        name: 'Forest',
        meta: {
          title: "林木管理"
        },
        component: Forest
      },
      {
        path: '/trace/symptom',
        name: 'Symptom',
        meta: {
          title: "病害管理"
        },
        component: Symptom
      },
      {
        path: '/trace/plant',
        name: 'Plant',
        meta: {
          title: "树木管理"
        },
        component: Plant
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    name: 'Login',
    component: Login
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {

  if (to.path === '/login') return next()
  // 将要访问的不是login， 先获取token判断是否有权限, 如果token不存在， 就强制跳转到login， 否则放行
  const tokenStr = window.sessionStorage.getItem('token')
  if (JSON.stringify(tokenStr).length < 1) return next('/login')
  next()
})
//
// const menuToRoute = (menu) => {
//   console.log("正在添加menu-->")
//   console.log(menu)
//   if (!menu.component) {
//     return null
//   }
//   // 复制属性
//   let route = {
//     name: menu.name,
//     path: menu.url,
//     meta: {
//       icon: menu.icon,
//       title: menu.title
//     }
//   }
//   route.component = () => import('@/views/modules/' + menu.component +'.vue')
//   return route
// }
export default router
